<div class="red" style="font-size: 14px; margin-bottom: 10px;">
    <div>注：如果是用户使用<bold>在线充值</bold>成功了，但是账号余额没有增加的情况，请不要在这里加款！</div>
    <div>【操作指引】在<bold>充值订单</bold>功能找到对应的订单，点击“<bold>标记成功</bold>”就可以。</div>
    <div>【惩罚扣款】例如要扣除用户100元，输入100即可，不要输入-100。</div>
</div>

<table class="form-table-without-line">
    <tr>
        <th>用户名</th>
        <td>
            <input type="text" class="am-form-field username" placeholder="请输入用户名">
            <div class="clue balance">当前余额：<span class="red">--</span></div>
        </td>
    </tr>

    <tr>
        <th>操作类型</th>
        <td>
            <div>
                <label class="am-radio-inline client_deposit">
                    <input type="radio" name="type" value="0" data-am-ucheck checked> 人工充值
                </label>
                <label class="am-radio-inline client_bonus">
                    <input type="radio" name="type" value="1" data-am-ucheck> 奖励发放
                </label>
                <label class="am-radio-inline account_penalty">
                    <input type="radio" name="type" value="2" data-am-ucheck> 惩罚扣款
                </label>
                <label class="am-radio-inline account_deduct">
                    <input type="radio" name="type" value="3" data-am-ucheck> 其他扣款
                </label>
            </div>
        </td>
    </tr>

    <tr>
        <th>金额</th>
        <td>
            <input type="number" class="am-form-field amount" placeholder="请输入金额，元">
        </td>
    </tr>

    <tr>
        <th>备注</th>
        <td>
            <textarea placeholder="备注，最多60个文字" class="memo" maxlength="120" rows="5"></textarea>
            <button class="btn-charge">提交</button>
            <div class="clue">注意：提交成功后，请到“充值记录”或“扣款记录”中进行二次审核</div>
        </td>
    </tr>
</table>

<script>
    Util.applyRights({
        "/client/deposit": "client_deposit",
        "/client/bonus": "client_bonus",
        "/account/penalty": "account_penalty",
        "/account/deduct": "account_deduct",
    });

    // 查找用户信息
    var userinfo = context.center.user;
    var _pullUserInfo = function() {
        var n = $(".username").val().trim();
        if (n == '') return;
        CMD.find_user_by_name(n, function(r) {
            if (r.list.length == 0) {
                $(".username").val("");
                alert2("用户" + n + "不存在！", true);
                return;
            }

            userinfo = r.list[0];
            fillUserInfoForBalance();
        });
    }
    
    function fillUserInfoForBalance() {
        $(".username").val(userinfo.mobile);
        $(".btn-charge").removeAttr("disabled");
        for (var i in userinfo.account) {
            if (userinfo.account[i].owner == "") {
                $(".balance .red").html((userinfo.account[i].balance/100).toFixed(2));
                return;
            }
        }
        $(".balance .red").html(userinfo.balance/100);
    }
    if (userinfo) {
        fillUserInfoForBalance();
        _pullUserInfo();
    }

    $(".username").change(function() {
        $(".btn-charge").attr("disabled", true);
        $(".balance .red").html("--");

        // 查找用户信息
        userinfo = null;
        _pullUserInfo();
    });

    $(".btn-charge").click(function() {
        if (!userinfo) return;
        var amount = $(".amount").val().trim();
        if (amount == '') {
            alert2("金额不对！", true);
            return;
        }
        amount = amount * 1;
        if (amount < 0.01) {
            alert2("金额至少为0.01！", true);
            return;
        }

        var type = $('input:radio:checked').val();
        var channel = 'manual';
        var memo = {
            info: $(".memo").val().trim(),
            time: new Date().Format("yyyy-MM-dd HH:mm:ss"),
            requestAccount: context.user.user.mobile,
        }
        if (type == '1') {
            startLoading();
            CMD.reward(userinfo.id, '', parseInt(amount * 100), memo, function(r) {
                stopLoading();
                if (!r) alert2("操作失败，请到“账变记录”中进行二次确认！", true);
                else
                    alert2("提交成功，请到“账变记录”中进行二次审核！");
            });
            return;
        }
        if (type == '2') {
            startLoading();
            CMD.account_penalty(userinfo.id, '', parseInt(amount * 100), memo, function(r) {
                stopLoading();
                if (!r) alert2("操作失败，请到“扣款记录”中进行二次确认！", true);
                else
                    alert2("提交成功，请到“扣款记录”中进行二次审核！");
            });
            return;
        }
        if (type == '3') {
            startLoading();
            CMD.account_deduct(userinfo.id, '', parseInt(amount * 100), memo, function(r) {
                stopLoading();
                if (!r) alert2("操作失败，请到“账变记录”中进行二次确认！", true);
                else
                    alert2("提交成功，请到“账变记录”中进行二次审核！");
            });
            return;
        }

        startLoading();
        CMD.deposit(userinfo.id, "", amount * 100, "handOper", channel, memo, function(r) {
            stopLoading();
            alert2("提交成功，请到“线下充值”中进行二次审核后，才能到账！");
        }, function() {
            stopLoading();
            alert2("充值失败！", true);
        });
    });
</script>
